#include<bits/stdc++.h>
using namespace std;

int st[100005][22];

inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

signed main() {
	int n, m;
	n = read();
	m = read();
	for (int i = 1; i <= n; i++)
//		cin >> st[i][0];
		st[i][0] = read();
	for (int j = 1; j <= 17; j++) {
		for (int i = 1; i + (1 << j) - 1 <= n; i++)
			st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
	}
	int l, r;
	for (int i = 1; i <= m; i++) {
		l = read();
		r = read();
		int k = log2(r - l + 1);
		printf("%d\n", max(st[l][k], st[r - (1 << k) + 1][k]));
//		cout << max(st[l][k], st[r - (1 << k) + 1][k]) << endl;
	}
	return 0;
}
